package com.rockcent.mall.lib.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.PageResult;
import com.rockcent.common.common.SQLObjectDoing;
import com.rockcent.common.common.SQLPageDoing;
import com.rockcent.common.utils.RDateUtils;
import com.rockcent.mall.lib.domain.enumclass.MallAuditCertifyInfoStatusType;
import com.rockcent.mall.lib.dto.MallCertifyInfoDTO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Arrays;

/**
 * Created by Marco on 16-11-13.
 */
@Repository
public class MallCertifyInfoRepositoryImpl implements MallCertifyInfoRepositoryCustom{

    private static final Logger log = LoggerFactory.getLogger(MallCertifyInfoRepositoryImpl.class);

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public PageResult<MallCertifyInfoDTO> mallAptitudeInfoPage(String mallName, String mobile, String beginTime, String endTime, String status, PageResult page) {
        SQLPageDoing<MallCertifyInfoDTO> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("select %s from mall_certify_info mc left join mall m on mc.mall_id = m.id left join auth_user au on m.account_id = au.id where 1=1");
            if (StringUtils.isNotBlank(mallName)) {
                sql.append(" and m.name like :mallName");
                params.put("mallName", "%" + mallName + "%");
            }
            if (StringUtils.isNotBlank(mobile)) {
                sql.append(" and au.mobile like :mobile");
                params.put("mobile", "%" + mobile + "%");
            }
            if (StringUtils.isNotBlank(status)) {
                if (MallAuditCertifyInfoStatusType.ALL == MallAuditCertifyInfoStatusType.valueOf(status)) {
                    sql.append(" and m.audit_certify_info_status in (:status)");
                    params.put("status", Arrays.asList(MallAuditCertifyInfoStatusType.AUDIT_PASS.toString(), MallAuditCertifyInfoStatusType.NOT_AUDIT.toString()));
                }else {
                    sql.append(" and m.audit_certify_info_status = :status");
                    params.put("status", status);
                }
            }
            if (StringUtils.isNotBlank(beginTime) && StringUtils.isNotBlank(endTime)) {
                if (MallAuditCertifyInfoStatusType.AUDIT == MallAuditCertifyInfoStatusType.valueOf(status)) {
                    sql.append(" AND mc.date_created < :endDate AND mc.date_created > :beginDate");
                }else {
                    sql.append(" AND m.date_audited < :endDate AND m.date_audited > :beginDate");
                }
                params.put("beginDate", RDateUtils.beforeDateStr(beginTime, 0));
                params.put("endDate", RDateUtils.afterDateStr(endTime, 1));
            }

            if (MallAuditCertifyInfoStatusType.AUDIT == MallAuditCertifyInfoStatusType.valueOf(status)) {
                sql.append(" order by mc.date_created desc");
            } else {
                sql.append(" order by m.date_audited desc");
            }
            fields.append("mc.*, au.mobile mobile, au.email email, m.name mallName, m.audit_certify_info_status status, m.date_audited dateAudited");
        };
        return doing.go(page, MallCertifyInfoDTO.class, objectMapper, jdbcTemplate, log);
    }

    @Override
    public MallCertifyInfoDTO findMallCertifyInfoById(Long id) {
        SQLObjectDoing<MallCertifyInfoDTO> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("select %s from mall_certify_info mc left join mall m on mc.mall_id = m.id left join auth_user au on m.account_id = au.id where mc.id = :id");
            params.put("id", id);
            fields.append("mc.*, au.mobile mobile, m.name mallName, m.audit_certify_info_status status");
        };
        return doing.go(MallCertifyInfoDTO.class, jdbcTemplate, log, objectMapper);
    }
}
